﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace scomA2Proj
{
    [Serializable]
    public class Order
    {
        /// <summary>
        /// The sender id. This should be the thread id.
        /// </summary>
        public int SenderID { get; set; }
        /// <summary>
        /// Credit card number
        /// </summary>
        public int CardNo { get; set; }
        /// <summary>
        /// Number of chickens.
        /// </summary>
        public int Amount { get; set; }
        /// <summary>
        /// The DateTime the order was submitted by retailer.
        /// </summary>
        //public DateTime OrderTime { get; set; }
        public Stopwatch stopwatch;

        private Order() { }

        /// <summary>
        /// Creates an order
        /// </summary>
        /// <param name="senderID">The sender id(thread id).</param>
        /// <param name="cardno">the credit card number.</param>
        /// <param name="amount">The number of chickens to order.</param>
        public Order(Stopwatch stopwatch, int senderID, int cardno, int amount)
        {
            this.stopwatch = stopwatch;
            //this.OrderTime = orderTime;
            this.SenderID = senderID;
            this.CardNo = cardno;
            this.Amount = amount;
        }

        /// <summary>
        /// Encode an order to a string as xml.
        /// </summary>
        /// <param name="order">The order to encode</param>
        /// <returns>XML string of the order.</returns>
        public static string Encode(Order order)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Order));
            StringWriter s = new StringWriter();
            ser.Serialize(s, order);
            return s.ToString();
        }

        /// <summary>
        /// Decode an order from a string into Order object
        /// </summary>
        /// <param name="order">The xml string order.</param>
        /// <returns>The decoded Order object.</returns>
        public static Order Decode(string order)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Order));
            StringReader r = new StringReader(order);
            return (Order)ser.Deserialize(r);
        }

        /// <summary>
        /// Determins whether or not all attributes of an Order equals all attributes of another Order.
        /// </summary>
        /// <param name="obj">Other object to compare.</param>
        /// <returns>True if all attributes are equal.</returns>
        public override bool Equals(object obj)
        {
            Order other = (Order)obj;
            return other.Amount == Amount && other.CardNo == CardNo && other.SenderID == SenderID;
        }

        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <returns>integer hash of the object.</returns>
        public override int GetHashCode()
        {
            return (Amount.ToString() + CardNo.ToString() + SenderID.ToString()).GetHashCode();
        }
    }
}
